<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once 'Savant2/Plugin.php';

class Savant2_Plugin_leagueman_list extends Savant2_Plugin{
	
	function plugin($method)
	{
		if (substr(0, 1) != "_" &&method_exists($this, $method)) {
			
			$args = func_get_args();
			array_shift($args);
			
			return call_user_func_array(array(&$this, $method), $args);
		}
	}
	
	function _getData($sql){
		global $database;
		
		$database->setQuery($sql);
		return $database->loadObjectList();
	}
	
	function section_category(){
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$sql ="SELECT c.id AS cid, c.title AS ctitle, s.id AS sid, s.title AS stitle
				 FROM #__categories c
		   INNER JOIN #__sections s ON c.section = s.id AND s.scope = 'content'";
        
        $rows = $this->_getData($sql);
		
		$input = '<select name="' . $name . '" class="inputbox" size="1" ' . $onchange . '>';
		$input .= '<option value="">Select Content Category</option>';
		
		$sid = 0;
		foreach($rows AS $row){
			if($row->sid != $sid){
				$sid = $row->sid;
				$input.= '<optgroup label="'.$row->stitle.'">';
			}
			
			$value = sprintf("%s:%s", $row->sid, $row->cid);
			if($value == $default){
				$selected = 'selected="selected"';
			} else {
				$selected = '';
			}
			
			$input.= '<option value="' . $value . '" '. $selected .'>' . $row->ctitle . '</option>';
		}
		
		$input.= '</select>';
		
		return $input;
	
	}
	
	function categories(){
	
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$categories[] = mosHTML::makeOption(0, "Select League");
		
		$sql ="SELECT id AS value, title AS text
			 	 FROM #__categories
				WHERE section = 'com_leagueman'";
        
        $categories = array_merge($categories, $this->_getData($sql));
		
		$settings[] = 'class="inputbox"';
		$settings[] = 'size="1"';
		$settings[] = 'style="width:200px;"';
		
		return mosHTML::selectList($categories, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function clubs(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$clubs[] = mosHTML::makeOption(0, "Select Club");
		
		$sql = "SELECT id AS value, name AS text 
				  FROM #__leagueman_club
			  ORDER BY name";
		
		$clubs = array_merge( $clubs, $this->_getData($sql));
		
		return mosHTML::selectList($clubs, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function projects(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$projects[] = mosHTML::makeOption(0, "Select Project");
		
		$sql = "SELECT p.project_id AS value, 
					   p.project_name AS text 
				  FROM #__leagueman_project p
			 LEFT JOIN #__categories c ON c.id = p.project_category
				 WHERE p.published = 1 AND p.finished = 0
			  ORDER BY c.ordering";
		
		$projects = array_merge( $projects, $this->_getData($sql));
		
		return mosHTML::selectList($projects, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
		
	}
	
	function seasons(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$seasons[] = mosHTML::makeOption(0, "Select Season");
		
		$sql = "SELECT season_id AS value, 
					   season_name AS text 
				  FROM #__leagueman_season 
			  ORDER BY ordering";
		
		$seasons = array_merge( $seasons, $this->_getData($sql));
		
		return mosHTML::selectList($seasons, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function leagues(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$leagues[] = mosHTML::makeOption(0, "Select League");
		
		$sql = "SELECT league_id AS value, 
					   league_name AS text 
				  FROM #__leagueman_league 
			  ORDER BY ordering";
		
		$leagues = array_merge( $leagues, $this->_getData($sql));
		
		return mosHTML::selectList($leagues, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function templates(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$types[] = mosHTML::makeOption("", "Select Projecttype");
		
		$sql = "SELECT type_template AS value, 
					   type_name AS text 
				  FROM #__leagueman_projecttype";
		
		$types = array_merge( $types, $this->_getData($sql));
		
		return mosHTML::selectList($types, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function types(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$types[] = mosHTML::makeOption(0, "Select Projecttype");
		
		$sql = "SELECT type_id AS value, 
					   type_name AS text 
				  FROM #__leagueman_projecttype";
		
		$types = array_merge( $types, $this->_getData($sql));
		
		return mosHTML::selectList($types, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function users(){
		
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$users[] = mosHTML::makeOption( '0', 'Select User' );
		
		$sql = "SELECT id AS value, 
					   name AS text
				  FROM #__users";
		$users = array_merge( $users, $this->_getData($sql));
		
		return mosHTML::selectList($users, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	}
	
	function finish(){
	
		$args = func_get_args();
		
		$name		= $args[0];
		$default 	= $args[1];
		$submitting = isset($args[2]) ? $args[2] : false;
		
		if($submitting){
			$onchange = 'onChange="javascript:document.adminForm.submit();"';
		} else {
			$onchange = '';
		}
		
		$list[] = mosHTML::makeOption(0, "not finished Projects");
		$list[] = mosHTML::makeOption(1, "finished Projects");
		
		return mosHTML::selectList($list, $name, 'class="inputbox" size="1"' . $onchange, 'value', 'text', $default);
	
	}
}
?>